package com.ruan.service;

import com.ruan.dao.UserMapper;
import com.ruan.entity.Result;
import com.ruan.entity.User;
import com.ruan.openfeign.CartCenterOpenfeign;
import com.ruan.openfeign.RoleCenterOpenfeign;
import com.ruan.query.UserQuery;
import com.ruan.utils.Auth0JwtUtils;
import com.ruan.utils.LongAuth0JwtUtils;
import com.ruan.utils.RedisUtil;
import com.ruan.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName:UserService
 * @Author:阮昇
 * @Description:
 * @Datetime: 2023/6/29 11:00
 **/
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private RoleCenterOpenfeign roleCenterOpenfeign;
    @Autowired
    private CartCenterOpenfeign cartCenterOpenfeign;
    public Result login(User user1) {

        User user = userMapper.getUserByName(user1.getUserName());

        if(user == null){

            return new Result(100,"用户名错误");

        }else {

            if(!user1.getUserPwd().equals(user.getUserPwd())){

                return new Result(100,"密码错误");

            }

            Map<String,Object> map = new HashMap<>();

            map.put("userId",user.getId());

            String token = Auth0JwtUtils.sign(map);

            String refreshToken = LongAuth0JwtUtils.sign(map);

            redisUtil.set("userId", String.valueOf(user.getId()));

            redisUtil.set("refreshToken",refreshToken);

            return new Result(200,"登陆成功",user,token,refreshToken);

        }
    }

    public Result register(User user1) {
        User user = userMapper.getUserByName(user1.getUserName());
        if(user != null){
            return new Result(100,"该用户已被注册");
        }
        int row = userMapper.register(user1);
        if(row != 1){
            return new Result(100,"注册失败");
        }
        return new Result(200,"注册成功");
    }

    public Result addUser(User user1) {
        User user = userMapper.getUserByName(user1.getUserName());
        if(user != null){
            return new Result(100,"该用户已被注册");
        }
        int row = userMapper.addUser(user1);
        if(row != 1){
            return new Result(100,"添加失败");
        }
        return new Result(200,"添加成功");
    }

    public List<UserVo> getUserList(UserQuery userQuery) {
        List<UserVo> userVoList = userMapper.getUserList(userQuery);
        for (UserVo i:userVoList){
            i.setRoleName(roleCenterOpenfeign.getRoleById(i.getRoleId()).getRoleName());
        }
        return userVoList;
    }

    public Result editUser(User user1) {
        User user = userMapper.getUserByName(user1.getUserName());
        if(user != null&&!user.getId().equals(user1.getId())){
            return new Result(100,"该用户已被注册");
        }
        int row = userMapper.editUser(user1);
        if(row != 1){
            return new Result(100,"修改失败");
        }
        return new Result(200,"添加成功");
    }

    @Transactional
    public Result deleteUser(Integer id) {
        int row = userMapper.deleteUser(id);
        cartCenterOpenfeign.deleteCartByUserId(id);
        if(row != 1){
            return new Result(100,"删除失败");
        }
        return new Result(200,"删除成功");
    }

    @Transactional
    public Result deleteUserByIds(Integer[] ids) {
        int row = userMapper.deleteUserByIds(ids);
        cartCenterOpenfeign.deleteCartByUserIds(ids);
        if(row != ids.length){
            return new Result(100,"删除失败");
        }
        return new Result(200,"删除成功");
    }
}
